<?php

App::uses('AppController', 'Controller');

/**
 * Students Controller
 *
 * @property Student $Student
 */
class StudentsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index($branch_id = null) {
        Controller::loadModel('Stage');
        $this->Stage->recursive = 1;
        //$stages=$this->Stage->find("list",array("order"=>"level"));
        $stages_branche = $this->Stage->find("all", array("order" => "level"));
        $this->set("stages_branche", $stages_branche);

        if (!$branch_id) {
            Controller::loadModel('Uniyear');
            $uniyears = $this->Uniyear->find('list');
            $this->set(compact('uniyears'));
            $this->render("stages_view");
        } else {
            Controller::loadModel('Branch');
            $this->Branch->recursive = 1;
            $branche = $this->Branch->read(null, $branch_id);
            $this->set(compact('branche', $branche));
            $this->Student->recursive = 0;
            $this->paginate=array("Student"=>array(
                "conditions"=>array("Student.id in (select student_id from studentbranches where branch_id=$branch_id and uniyear_id={$this->config["current_year_id"]})"),
            ));
            
            $this->set('students', $this->paginate());
            
            $this->set("current_year",$this->config["current_year_name"]);
        }


        
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->GetOption('student_fields'); //get setting of student_fields
        $this->Student->id = $id;
        if (!$this->Student->exists()) {
            throw new NotFoundException(__('Invalid data'));
        }
        $this->set('student', $this->Student->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($branch_id = null) {
		$this->GetOption('student_fields'); //get setting of student_fields
        Controller::loadModel('Stage');
        $this->Stage->recursive = 1;
        //$stages=$this->Stage->find("list",array("order"=>"level"));
        $stages_branche = $this->Stage->find("all", array("order" => "level"));
        $this->set("stages_branche", $stages_branche);
        if ($this->request->is('post')) {

            if (isset($this->request->data["Student"]["user_id"])) {
                $useId = AuthComponent::user('id');

                $this->request->data["Student"]["user_id"] = $useId;
            }

            $this->Student->create();
            if ($this->Student->save($this->request->data)) {
                $id = $this->Student->id;
                $this->savelog($id);
                $useId = AuthComponent::user('id');
                Controller::loadModel('Studentbranch');
                $student_branch["Studentbranch"] = array(
                    'student_id' => $id,
                    'branch_id' => $branch_id,
                    'uniyear_id' => $this->config["current_year_id"],
                    'user_id' => $useId
                );

                $this->Studentbranch->save($student_branch);

                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'add', $branch_id));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        }

        $branches = $this->Student->Branch->find('list');
        $users = $this->Student->User->find('list');
        $countries = $this->Student->Country->find('list',array("fields"=>array("id","country_ar")));
        $this->set(compact('branches', 'users', 'countries'));
        if (!$branch_id) {
            $this->render("stages");
        } else {
            Controller::loadModel('Branch');
            $this->Branch->recursive = 1;
            $branche = $this->Branch->read(null, $branch_id);
            $this->set(compact('branche', $branche));
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
    	$this->GetOption('student_fields'); //get setting of student_fields
        $this->Student->id = $id;
        if (!$this->Student->exists()) {
            throw new NotFoundException(__('Invalid student'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {

            if (isset($this->request->data["Student"]["user_id"])) {
                $useId = AuthComponent::user('id');
                // $useId ="1";				
                $this->request->data["Student"]["user_id"] = $useId;
            }

            if ($this->Student->save($this->request->data)) {
                $this->savelog($id);
                $this->Session->setFlash(__('Data has been saved'), 'default', array("class" => "mws-form-message success"));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default', array("class" => "mws-form-message error"));
            }
        } else {
            $this->request->data = $this->Student->read(null, $id);
        }
        $branches = $this->Student->Branch->find('list');
        $users = $this->Student->User->find('list');
        $countries = $this->Student->Country->find('list',array("fields"=>array("id","country_ar")));
        $this->set(compact('branches', 'users', 'countries')); 
 	    
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Student->id = $id;
        if (!$this->Student->exists()) {
            throw new NotFoundException(__('Invalid student'));
        }
        if ($this->Student->delete()) {
            $this->savelog($id);

            $this->Session->setFlash(__('Data has been deleted'), 'default', array("class" => "mws-form-message success"));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

}
